<template>
  <div class="system-user-container standard-container layout-padding layout-pd">
    <div class="review-list-title">
      <div class="title-actions">
        <el-button size="default" type="primary" class="ml10" @click="getTableData">
          <el-icon>
            <ele-Search/>
          </el-icon>
          查询
        </el-button>
      </div>
    </div>
    <el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
      <el-table-column label="用户信息" width="220">
        <template #default="scope">
          <div class="flex">
            <div>
              <el-avatar :size="50" :src="STATIC_URL + scope.row.avatar"/>
            </div>
            <div class="my-flex-center">
              {{ scope.row.nickname }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" show-overflow-tooltip></el-table-column>
      <el-table-column prop="mobile" label="手机号" show-overflow-tooltip></el-table-column>
      <el-table-column prop="amount" label="总消费" show-overflow-tooltip></el-table-column>
      <el-table-column prop="score" label="积分" show-overflow-tooltip></el-table-column>
      <!--			<el-table-column prop="balance" label="余额" show-overflow-tooltip></el-table-column>-->
      <el-table-column prop="registration" label="注册时间" show-overflow-tooltip width="200"></el-table-column>
      <el-table-column prop="lastLoginTime" label="上次登录时间" show-overflow-tooltip width="200"></el-table-column>
      <el-table-column label="操作" show-overflow-tooltip width="140">
        <template #default="scope">
          <el-button size="small" text type="primary" @click="view(scope.row.id)">查看</el-button>
          <el-button size="small" text type="primary" @click="user_dynamic(scope.row.id)">会员动态</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
          :page-sizes="[10, 15, 20, 30, 50, 100]"
          v-model:current-page="state.tableData.paging.currentPage"
          background
          v-model:page-size="state.tableData.paging.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.paging.totalCount"
      >
      </el-pagination>
    </div>
    <Dialog ref="DialogRef"></Dialog>
    <DialogDynamic ref="DialogDynamicRef"></DialogDynamic>
  </div>
</template>

<script setup lang="ts">
import {defineAsyncComponent, onMounted, reactive, ref} from 'vue';
import {STATIC_URL} from '/@/utils/config';
import {ClientUser} from '/@/api/shop/user';

const Dialog = defineAsyncComponent(() => import('./dialog.vue'));
const DialogRef = ref();

const DialogDynamic = defineAsyncComponent(() => import('./dialog_dynamic.vue'));
const DialogDynamicRef = ref();
const state = reactive({
  isToggle: false,
  search: {
    name: '',
    searchDateTime: '',
  },
  tableData: {
    data: [],
    total: 0,
    loading: false,
    paging: {
      currentPage: 1,
      pageSize: 15,
      totalCount: 0,
      totalPages: 0,
    },
  },
});

// 分页改变
const onHandleSizeChange = (pageSize: number) => {
  state.tableData.paging.pageSize = pageSize;
  getTableData();
};

// 分页改变
const onHandleCurrentChange = (currentPage: number) => {
  state.tableData.paging.currentPage = currentPage;
  getTableData();
};

const getTableData = async () => {
  const {data} = await ClientUser().search({
    offset: (state.tableData.paging.currentPage - 1) * state.tableData.paging.pageSize,
    limit: state.tableData.paging.pageSize,
  });
  state.tableData.data = data.datas;
  state.tableData.paging.currentPage = data.paging.currentPage;
  state.tableData.paging.totalCount = data.paging.totalCount;
  console.log('data: ', data);
};
const view = (id: string) => {
  DialogRef.value.openDialog(id);
};

const user_dynamic = (id: string) => {
  DialogDynamicRef.value.openDialog(id);
};
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
  .el-table {
    flex: 1;
  }
}

.my-avatar {
  width: 100%;
  height: 100%;
}

.my-flex-center {
  padding-right: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
  padding-left: 5px;
}
</style>
